const util = require('../../../../utils/util.js');
const api = require('../../../../config/api.js');
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    pcatalogid: '',
    pcatalogname: '',
    tabsActive: 0,// 选中的下标
    list: []// 专辑数据
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this;
    var pcatalogid = options.pcatalogid;
    var pcatalogname = options.pcatalogname;
    if (pcatalogname == null || pcatalogname.length <= 0) {
      pcatalogname = "全部"
    }
    _this.setData({
      pcatalogid: pcatalogid,
      pcatalogname: pcatalogname
    })
    wx.setNavigationBarTitle({
      title: pcatalogname
    });

    // 获取二级类目的专辑信息
    _this.levelTwoCategoryWeeklyInfo();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * tabs切换
   */
  tabsChange(event) {
    let that = this;
    var index = event.detail.index;
    var list = that.data.list;
    for (var i = 0; i < list.length; i++) {
      if(i == index){
        that.setData({
          ["list[" + i + "].color"]: '#fe7f0c'
        });
      }else{
        that.setData({
          ["list[" + i + "].color"]: '#000'
        });
      }
    }
  },

  /**
   * 专辑详情
   */
  toDetailsAlbum: function(){
    wx.navigateTo({
      url: '../detailsAlbum/detailsAlbum',
    })
  },

  /**
   * 获取二级类目的专辑信息
   */
  levelTwoCategoryWeeklyInfo: function(){
    var _this = this;
    var pcatalogid = _this.data.pcatalogid;
    var pcatalogname = _this.data.pcatalogname;
    var data = {};
    data.pcatalogid = pcatalogid;
    data.pcatalogname = pcatalogname;
    var url = api.Home_LevelTwoCategoryWeeklyInfoUrl;
    var openid = app.globalData.logininfo.openid;
    if (openid != null && openid != "") {
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        data.openid = openid;
        data.access_token = res.access_token;
        url = api.Home_LevelTwoCategoryWeeklyInfoApiUrl;
        _this.levelTwoCategoryWeeklyInfoQuery(url, data);
      })
    } else {
      _this.levelTwoCategoryWeeklyInfoQuery(url, data);
    }
  },

  /**
   * 获取二级类目的专辑信息：发送请求
   */
  levelTwoCategoryWeeklyInfoQuery: function (url, data) {
    var _this = this;
    util.post(url, data).then(function (res2) {
      if (res2.code == 200) {
        var list = res2.result.list;
        _this.setData({
          list: list
        })
      }
    })
  },
})